<!-- /admin/dashboard.php -->
<?php
require_once(__DIR__ . '/../common/auth.php');
check_admin_login();
require_once(__DIR__ . '/../common/db.php');

// 数据统计
$total_messages    = $db->querySingle("SELECT COUNT(*) FROM messages");
$approved_messages = $db->querySingle("SELECT COUNT(*) FROM messages WHERE status=2");
$replied_messages  = $db->querySingle("SELECT COUNT(*) FROM messages WHERE status=3");
$rejected_messages = $db->querySingle("SELECT COUNT(*) FROM messages WHERE status=4");

// 部门分布统计（允许空值展示为“未填写”）
$dept_counts = [];
$res = $db->query("SELECT COALESCE(department, '未填写') AS department, COUNT(*) AS cnt FROM messages GROUP BY department ORDER BY cnt DESC");
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
    $dept_counts[] = $row;
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>仪表台 - 工会留言板后台</title>
    <link rel="icon" href="/assets/logo.ico?v=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/assets/style.css?v=<?php echo time(); ?>">
    <!-- 仪表台专属私有CSS -->
    <style>
        .main-flex { display: flex; }
        .dashboard-summary {
            padding: 36px 40px 36px 40px;
            width: 100%;
        }
        .dashboard-cards {
            display: flex;
            flex-wrap: wrap;
            gap: 28px;
            margin-bottom: 40px;
            justify-content: space-between;
        }
        .dashboard-card {
            flex: 1 1 150px;
            min-width: 150px;
            max-width: 240px;
            background: linear-gradient(120deg,#f5f8ff,#f0f6fe 80%);
            padding: 28px 0 20px 0;
            border-radius: 16px;
            box-shadow: 0 4px 18px #e9eefa;
            font-size: 1.13em;
            color: #285bb7;
            text-align: center;
            transition: box-shadow .2s;
        }
        .dashboard-card .number {
            font-size: 2.1em;
            font-weight: bold;
            color: #183a9c;
            margin-top: 12px;
        }
        .dept-table-area {
            margin-top: 40px;
            max-width: 400px;
        }
        .dept-table {
            border-collapse: collapse;
            width: 100%;
            background: #fafdff;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 2px 8px #edf1f9;
        }
        .dept-table th {
            background: #e6edff;
            color: #1a3685;
            font-weight: bold;
            font-size: 1.04em;
            padding: 10px 0;
        }
        .dept-table td {
            font-size: 1em;
            padding: 8px 0;
            color: #2c2c2c;
            border-bottom: 1px solid #e3e3f2;
        }
        .dept-table tr:last-child td { border-bottom: none; }
        .empty-table-tip {
            text-align: center;
            color: #aaa;
            padding: 18px 0;
        }
        @media (max-width: 1000px) {
            .main { padding: 18px 2vw 12px 2vw; }
            .dashboard-summary { padding: 16px 2vw 22px 2vw;}
            .dashboard-cards { gap: 10px; }
            .dept-table-area { max-width: 98vw; }
        }
    </style>
</head>
<body>
<?php include('../components/header.php'); ?>
<div class="main-flex">
    <?php include('../components/menu.php'); ?>
    <div class="main dashboard-summary">
        <h2 class="msg-title" style="text-align:center;margin-bottom:34px;">数据总览</h2>
        <div class="dashboard-cards">
            <div class="dashboard-card">
                总留言数
                <div class="number"><?php echo $total_messages; ?></div>
            </div>
            <div class="dashboard-card">
                已通过
                <div class="number"><?php echo $approved_messages; ?></div>
            </div>
            <div class="dashboard-card">
                已回复
                <div class="number"><?php echo $replied_messages; ?></div>
            </div>
            <div class="dashboard-card">
                已拒绝
                <div class="number"><?php echo $rejected_messages; ?></div>
            </div>
        </div>
        <div class="dept-table-area">
            <h3 style="font-size:1.08em;color:#285bb7;margin-bottom:10px;">部门分布</h3>
            <table class="dept-table">
                <tr><th>部门</th><th>留言数</th></tr>
                <?php if (count($dept_counts) === 0): ?>
                <tr><td colspan="2" class="empty-table-tip">暂无部门数据</td></tr>
                <?php else: ?>
                <?php foreach($dept_counts as $d): ?>
                <tr>
                    <td><?php echo htmlspecialchars($d['department']); ?></td>
                    <td><?php echo $d['cnt']; ?></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>
</body>
</html>
